#include "serial.h"
#include "lpc17xx_uart.h"
#include "lpc17xx_pinsel.h"
#include "lpc_types.h"

// Read options
int read_usb_serial_none_blocking(char *buf,int length)
{
	return(UART_Receive((LPC_UART_TypeDef *)LPC_UART0, (uint8_t *)buf, length, NONE_BLOCKING));
}

// Write options
int write_usb_serial_blocking(char *buf,int length)
{
	return(UART_Send((LPC_UART_TypeDef *)LPC_UART0,(uint8_t *)buf,length, BLOCKING));
}
// init code for the USB serial line
void serial_init(void)
{
	UART_CFG_Type UARTConfigStruct;			// UART Configuration structure variable
	UART_FIFO_CFG_Type UARTFIFOConfigStruct;	// UART FIFO configuration Struct variable
	PINSEL_CFG_Type PinCfg;				// Pin configuration for UART
	/*
	 * Initialize UART pin connect
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 0;
	PinCfg.Pinnum = 2;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 3;
	PINSEL_ConfigPin(&PinCfg);

	UART_ConfigStructInit(&UARTConfigStruct);

	UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);

	UART_Init((LPC_UART_TypeDef *)LPC_UART0, &UARTConfigStruct);	
	UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, &UARTFIFOConfigStruct);
	UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE);
	
}

/* SERIALAPI */
void serial_3pi_init(void)
{
  UART_CFG_Type      UARTConfigStruct     ;
  UART_FIFO_CFG_Type UARTFIFOConfigStruct ;
  PINSEL_CFG_Type    PinCfg               ;

  UART_ConfigStructInit(&UARTConfigStruct)       ;
  UARTConfigStruct.Baud_rate = (uint32_t) 115200 ;

  UART_FIFOConfigStructInit (&UARTFIFOConfigStruct)                                ;
  UART_Init                 ((LPC_UART_TypeDef *)LPC_UART3, &UARTConfigStruct)     ;
  UART_FIFOConfig           ((LPC_UART_TypeDef *)LPC_UART3, &UARTFIFOConfigStruct) ;
  UART_TxCmd                ((LPC_UART_TypeDef *)LPC_UART3, ENABLE)                ;

  PinCfg.Funcnum   = 2      ;
  PinCfg.OpenDrain = 0      ;
  PinCfg.Pinmode   = 0      ;
  PinCfg.Portnum   = 0      ;
  PinCfg.Pinnum    = 0      ;
  PINSEL_ConfigPin(&PinCfg) ;
  PinCfg.Pinnum    = 1      ;
  PINSEL_ConfigPin(&PinCfg) ;

  UART_ConfigStructInit(&UARTConfigStruct)       ;
  UARTConfigStruct.Baud_rate = (uint32_t) 115200 ;

  UART_FIFOConfigStructInit (&UARTFIFOConfigStruct)                                ;
  UART_Init                 ((LPC_UART_TypeDef *)LPC_UART3, &UARTConfigStruct)     ;
  UART_FIFOConfig           ((LPC_UART_TypeDef *)LPC_UART3, &UARTFIFOConfigStruct) ;
  UART_TxCmd                ((LPC_UART_TypeDef *)LPC_UART3, ENABLE)                ;

}

int write_serial_3pi(char *buf,int length)
{
  return(UART_Send((LPC_UART_TypeDef *)LPC_UART3,buf,length, BLOCKING));  
}

